<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Crypt;
use DB;

class RegisterController extends Controller
{
    //
    // register注册页面
    //
    public function register()
    {
        return view('admin.register.register', ['title' => '注册用户']);
    }

    // insert添加用户方法
    public function insert(Request $request)
    {
        // dd($request -> all());
        // 验证规则
        $this->validate($request, [
            'hd_name' => 'required|min:5|max:18',
            'hd_pass' => 'required',
            'hd_repass' => 'same:hd_pass',
            'hd_sex' => 'required',
            'hd_phone' => 'digits:11',
            'hd_email' => 'email',
            'hd_pic' => 'image|max:2048',
        ],[
            // 自定位错误信息
            'hd_name.required' => '用户名不能为空!',
            'hd_name.min' => '用户名不能少于5位!',
            'hd_name.max' => '用户名不能大于18位!',
            'hd_pass.required' => '密码不能为空!',
            'hd_repass.same' => '两次密码不一致,请检查!',
            'hd_sex.required' => '请你输入性别',
            'hd_phone.digits' => '请输入11位正确手机号!',
            'hd_email.email' => '请输入正确邮箱格式!',
            'dh_pic.max' => '最大2M',
        ]);
        // dd($data);
        $data = $request -> except('_token', 'hd_repass');
        // dd($data);
        $userData['hd_name'] = $data['hd_name'];
        // 处理密码字段
        $userData['hd_pass'] = Crypt::encryptString($data['hd_pass']);
        $userData['hd_sex'] = $data['hd_sex'];
        $userData['hd_email'] = $data['hd_email'];
        // $userData['hd_pic'] = $data['hd_pic'];
        $userData['hd_phone'] = $data['hd_phone'];
        //处理头像
        if($request -> hasFile('hd_pic'))
        {
            if($request -> file('hd_pic') -> isValid())
            {
                $extension = $request -> file('hd_pic') -> getClientOriginalExtension();
                // echo $extension; 上一步获取图片后缀  方便后面拼接
                $filename = mt_rand(100000000, 999999999).'.'.$extension;
                $dir = './uploads/avatar/';
                $request -> file('hd_pic') -> move($dir, $filename);
                $userInfoData['hd_pic'] = '/uploads/avatar/'.$filename;
            }
        }else{
            $userInfoData['hd_pic'] = '/uploads/avatar/default.jpg';
        }
        $userData['hd_token'] = str_random(50);
        // 处理生成,更新时间
        $time = Date('Y-m-d H:i:s');
        $userData['created_at'] = $time;
        $userData['updated_at'] = $time;

        //添加数据
        $usersId = DB::table('users') -> insertGetId($userData);
        if($usersId)
        {
            $userInfoData['user_id']= $usersId;
            $userInfoData['created_at']= $time;
            $userInfoData['updated_at']= $time;

            $res = DB::table('user_info') -> insert($userInfoData);
            if($res)
            {
                return redirect('/Admin/login') -> with(['info' => '注册成功']);
            }else{
                return back() -> with(['info' => '注册失败']);
            }
        }
    }

}
